<template>
  <div style="width: 100%">
    <div id="deawer">
      <v-tableTitle :btnConfig="btnConfig"
                    @cancel="cancel"
                    @save="save"></v-tableTitle>
      <div class="detailBox">
        <v-detail ref="detail"></v-detail>
      </div>
      <div class="detailListBox"
           v-if="!newFlag">
        <v-detailList ref="detailList"></v-detailList>
      </div>
    </div>
  </div>

</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import skipMixin from "@/mixins/SkipMixin";
import config from "../Config"
import tableTitle from "commons/TableTitle";
import detail from "../details/Detail"
import detailList from "../detailsList/DetailList"
import bus from "../../../../../bus/bus";

export default {
  name: "",
  mixins: [baseMixin, skipMixin, config],
  data () {
    return {
      newFlag: false,
      routeData: {}
    };
  },
  computed: {
    btnConfig () {
      return [
        { label: this.$t('common.save'), certigier: this.saveStatus, invalid: "save" },
        { label: this.$t('common.return'), invalid: "cancel" },
      ]
    }
  },
  created () {
  },
  methods: {

    //保存按钮
    async save () {
      let detail = this.$refs.detail.detailData;
      if (!detail.wmwhse) {
        this.$message.error(this.$t('security.menuLevel') + this.$t('app.emptyMes'))
        return
      }
      if (!detail.isrf) {
        this.$message.error(this.$t('security.isRfMenu'))
        return
      }
      if (!detail.description) {
        this.$message.error(this.$t('security.menuName') + this.$t('app.emptyMes'))
        return
      }
      if (this.newFlag) {
        this.insetuMenu(detail);
        this.cancel();
        return;
      }

      //判断父节点有没有子类

      if (!this.newFlag && this.$refs.detailList.detailNav == '2') {
        let detailList = this.$refs.detailList.detailData;
        if (!detailList.wmwhse) {
          this.$message.error(this.$t('security.menuLevel') + this.$t('app.emptyMes'))
          return
        }
        if (!detailList.isrf) {
          this.$message.error(this.$t('security.isRfMenu') + this.$t('app.emptyMes'))
          return
        }
        if (!detailList.description) {
          this.$message.error(this.$t('security.menuName') + this.$t('app.emptyMes'))
          return
        }
        if (!detailList.url) {
          this.$message.error(this.$t('security.urlLocation') + this.$t('app.emptyMes'))
          return
        }
        //判断有没有子菜单
        this.$set(detail, 'isprocess', 0)
        this.insetuMenu(detail);

        this.$set(detailList, 'isprocess', 1)
        this.$api.post(`/insertUmenu`, detailList).then(res => {
          
            this.$message.success(res.returnMgr)
            this.$refs.detailList.detailNav = '1'
        })

      } else if (!this.newFlag && this.$refs.detailList.detailNav == '1') {
        if (this.$refs.detailList.$children[1].tableData.length > 0) {
          this.$set(detail, 'isprocess', 0)
        } else {
          this.$set(detail, 'isprocess', 1)
        }
        this.insetuMenu(detail);
        this.cancel();
      }

    },
    insetuMenu (data) {
      this.$api.post(`/insertUmenu`, data).then(res => {
        
          this.$message.success(res.returnMgr)
      })
    },
    //弹框取消
    cancel () {
      setTimeout(() => {
        this.$router.push({ name: 'umenuRun' });
      }, 500);
    },
  },
  mounted () {
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip;
      this.routeData = data.routeData;
      this.newFlag = data.newFlag;
    } else {
      this.routeData = this.$store.state.routeData;
      this.newFlag = this.$store.state.newFlag;
    }

  },
  watch: {},
  components: {
    "v-tableTitle": tableTitle,
    "v-detail": detail,
    "v-detailList": detailList,
  }
}
</script>

<style scoped>
</style>
